﻿using System;
using System.Collections.Generic;
using System.Text;
using Aga.Controls.Tree;
using FlexminAPI;

namespace FlexminAdmin.Tree
{
	internal class FlexminTreeModel : TreeModel
	{
		public FlexminTreeModel()
			: base()
		{
		}

		public void Clear()
		{
			foreach (Node node in this.Nodes)
			{
				CloseConnectionsInNode(node);
			}
			this.Nodes.Clear();

			GroupTreeNode rootGroup = new GroupTreeNode(Properties.Resources.rootNodeName);
			this.Nodes.Add(rootGroup);
		}

		public void CloseConnectionsInNode(Node node)
		{
			if (node is ComputerTreeNode)
			{
				ComputerTreeNode computerNode = node as ComputerTreeNode;
				IHost computer = computerNode.Host;
				if (computer != null)
				{
					computer.ShutDown();
				}
				return;
			}

			foreach (Node subnode in node.Nodes)
			{
				CloseConnectionsInNode(subnode);
			}
		}
	}
}
